package com.wanghaixin.controller;

import com.wanghaixin.dto.ArticleDTO;
import com.wanghaixin.dto.DataGridResult;
import com.wanghaixin.dto.QueryDTO;
import com.wanghaixin.pojo.Article;
import com.wanghaixin.service.ArticleService;
import com.wanghaixin.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Auther: 王海新
 * @Date: 2020/11/1 09:14
 * @Description: 咨询管理controller层
 */
@Controller
public class ArticleController {

    @Autowired
       private ArticleService articleService;

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  9:17  2020/11/1
     * @Version:  1.0.0
     * @Description:  分页管理
     */
    @RequestMapping("/sys/article/list")
    @ResponseBody
    public DataGridResult findByPage(QueryDTO queryDTO){
        DataGridResult byPage = articleService.findByPage(queryDTO);
        return byPage;
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  9:19  2020/11/1
     * @Version:  1.0.0
     * @Description:  删除操作
     */
    @RequestMapping("/sys/article/del")
    @ResponseBody
    public R delArticle(@RequestBody List<Long> ids){
        for (Long id : ids) {
            int i = articleService.delArt(id);
        }
        return R.ok();
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  9:24  2020/11/1
     * @Version:  1.0.0
     * @Description:  根据id查数据
     */
    @RequestMapping("/sys/article/info/{id}")
    @ResponseBody
    public R findById(@PathVariable("id") long id){
        Article byID = articleService.findByID(id);
        return R.ok().put("article",byID);
    }
    
    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  9:27  2020/11/1
     * @Version:  1.0.0
     * @Description:  添加操作
     */
    @RequestMapping("/sys/article/save")
    @ResponseBody
    public R addAtricle(@RequestBody ArticleDTO articleDTO){
        int i = articleService.addArticle(articleDTO);
        return i>0 ? R.ok(): R.error("添加失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  9:31  2020/11/1
     * @Version:  1.0.0
     * @Description:  修改操作
     */
    @RequestMapping("/sys/article/update")
    @ResponseBody
    public R updateArticle(@RequestBody ArticleDTO articleDTO){
        int i = articleService.updateArticle(articleDTO);
        return i>0 ? R.ok(): R.error("修改失败");
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  9:52  2020/11/1
     * @Version:  1.0.0
     * @Description:  文件上传功能
     */
    @RequestMapping("/ytupload")
    @ResponseBody
    //参数是文件上传的一个类
    public R ytupload(@RequestParam("mypic")MultipartFile multipartFile){
//得到文件名
        String originalFilename = multipartFile.getOriginalFilename();
        //上传目的地，不做分布式，只是存储到本地，不考虑文件名重复问题
        File dest = new File("C:\\Users\\黑白客\\Pictures\\Camera Roll"+originalFilename);
        try {
            multipartFile.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.ok().put("file",originalFilename);
    }
}
